import http from '@/utils/http/index'


export interface SearchHostForm {
  ip: string;
  guaProtocol: string;
  pageNum: number;
  pageSize: number;
}

export interface AddHostForm {
  uid: string;
  ip: string;
  guaPort: number;
  status: number
  guaProtocol: string
  clusterId: string
  bindUserId: string
  account: string
  password: string
  description: string
}

export interface CapHost {
  id: string
  uid: string
  ip: string
  guaPort: number
  status: number
  guaProtocol: string
  clusterId: string
  bindUserId: string
  lockedUserId: string
  meta: string
  createdAt: string
}

export interface CapCluster {
  id: string
  name: string
  cpuThreshold: number
  memoryThreshold: number
  maxSessionTime: number
  sessionOutTime: number
  timeOutAction: number
  clusterType: number
  creator: string
  creatorId: string
  createdAt: string
}

export interface HostDto {
  hostInfo: CapHost
  clusterInfo: CapCluster
}



export interface AddClusterForm {
  id: string
  name: string
  cpuThreshold: number
  memoryThreshold: number
  maxSessionTime: number
  sessionOutTime: number
  timeOutAction: number
  clusterType: number
}

export interface ClusterDto {
  clusterInfo: CapCluster
  hostList: CapHost[]
}

// 主机
export const addHost = (data: AddHostForm) => {
  return http.post('cap/host/add', data)
}
interface HostDtoPage {
  list: HostDto[];
  total: number
}
export const getHostPage = (params: SearchHostForm): Promise<HostDtoPage> => {
  return http.get('cap/host/page', { params })
}

export const delHost = (id: string) => {
  return http.delete('cap/host/del', { params: { id } })
}

export interface AddLinkDto {
  userId: string
  sourceId: string
  certifyId: string
  metaData: string
}

export const addLink = (data: AddLinkDto) => {
  return http.post('cap/link/add', data)
}

// 集群
export const addCluster = (data: AddClusterForm) => {
  return http.post('cap/cluster/add', data)
}

export const updateCluster = (data: AddClusterForm) => {
  return http.post('cap/cluster/edit', data)
}

export const getClusterList = (): Promise<CapCluster[]> => {
  return http.get('cap/cluster/list')
}
export const getClusterDtoList = (): Promise<ClusterDto[]> => {
  return http.get('cap/cluster/dtoList')
}
export const delCluster = (id: string) => {
  return http.delete('cap/cluster/del', { params: { id } })
}
export interface CapLink {
  id: string
  userId: string
  linkType: string
  sourceId: string
  metaData: string
  certifyId: string
}
export interface LinkDto {
  linkInfo: CapLink
  name: string
  img: string
  protocol: string
  title: string
  subtitle: string
}
// 应用、链接
export const getLinkList = (): Promise<LinkDto[]> => {
  return http.get('cap/link/list')
}
export const getLinkDetail = (id: string): Promise<LinkDto> => {
  return http.get('cap/link/detail', { params: { id } })
}
export interface AddSessionForm {
  linkId: string
}
export interface SearchSessionForm {
  protocol: string
  hostId: string
  status: string
  startTime: string
  endTime: string
  pageNum: number;
  pageSize: number;
}
export interface CapSession {
  id: string
  protocol: string
  hostId: string
  hostIp: string
  connectPort: number
  connectionId: string
  certifyId: string
  winSessionId: number
  width: string
  height: string
  status: number
  message: string
  createdAt: string
  connectedTime: string
  disconnectedTime: string
  creatorId: string
  clusterId: string
  creator: string
}
interface SessionDtoPage {
  list: CapSession[];
  total: number
}
interface OpenLinkDto {
  linkId: string
  sessionId: string
  width: string
  height: string
  dpi: string
}
// 会话
export const addSession = (linkId: string): Promise<string> => {
  return http.post('cap/session/add', { linkId })
}
export const getSessionPage = (params: SearchSessionForm): Promise<SessionDtoPage> => {
  return http.get('cap/session/page', { params })
}

export const sessionConn = (params: OpenLinkDto) => {
  return http.get('cap/session/conn', { params })
}
export interface CapCertify {
  id: string
  type: string
  sourceId: string
  account: string
  password: string
  description: string
  creator: string
}
interface CertifyDtoPage {
  list: CapCertify[];
  total: number
}
interface SearchCertifyForm {
  account: string
  type: string
  pageNum: number;
  pageSize: number;
}
export interface AddCertifyForm {
  type: string
  sourceId: string
  account: string
  password: string
  description: string
}
// 凭证
export const getCertifyList = (): Promise<CapCertify[]> => {
  return http.get('cap/certify/list')
}

export const getCertifyPage = (params: SearchCertifyForm): Promise<CertifyDtoPage> => {
  return http.get('cap/certify/page', { params })
}
export const delCertify = (id: string) => {
  return http.delete('cap/certify/del', { params: { id } })
}
export const addCertify = (data: AddCertifyForm) => {
  return http.post('cap/certify/add', data)
}

export interface CapAppCollect {
  id: string
  hostId: string
  appName: string
  icon: string
  linkPath: string
  exePath: string
  workSpace: string
  appParams: string
  pathStatus: boolean
  createTime: string
}

export interface AppCollectDto {
  appInfo: CapAppCollect
  hostInfo: CapHost
  clusterInfo: CapCluster
}

// cap专用操作
export const noticeHostRegister = (id: string) => {
  return http.post('cap/notice/register', { id })
}
export const noticeHostUploadApps = (id: string) => {
  return http.post('cap/notice/appUpload', { id })
}

export const getCapAppList = (): Promise<AppCollectDto[]> => {
  return http.get('cap/app/list')
}
export interface AppIssueForm {
  userIds: string[]
  clusterId: string
  appName: string
  exePath: string
  appParams: string
  workSpace: string
  icon: string
  isSingle: boolean
}

export const capAppIssue = (data: AppIssueForm) => {
  return http.post('cap/app/issue', data)
}

export const hostBindCluster = (hostId: string, clusterId: string) => {
  return http.post('cap/host/bindCluster', { hostId, clusterId })
}

export interface VmAppParams {
  appName: string
  hostSessionId: string
  linkId: string
  exePath: string
  workDir: string
  params: string[]
}

// getCapAppInfo 获取应用信息
export const getCapAppInfo = (linkId: string): Promise<VmAppParams> => {
  return http.post('cap/session/appInfo', { linkId })
}

export const insertOpenAppLogs = (linkId: string, sessionId: string, winSessionId: string) => {
  return http.post('cap/session/openApp', { linkId, sessionId, winSessionId })
}

export const bindWinSessionId = (sessionId: string, winSessionId: string) => {
  return http.post('cap/session/bindWinId', { sessionId, winSessionId })
}
